const {
    Router
} = require("express")
const Likeygood = require("../model/likeygood")
const Hotel = require("../model/Hotel")
const Tourism = require("../model/Tourism")
const Scenic = require("../model/Hotel")
const Delicacy = require("../model/Delicacy")

const router = new Router()

// 点赞的所有数据请求
router.get("/likeygoodList", async (req, res) => {
    const userId = req.userInfo._id
    // const list = await Hotel.find({}).limit(10*num);
    // 查询所有的该用户点赞信息
    const list = await Likeygood.find( { userId })
    res.status(200).json({list});
})

// 添加点赞的数据部分
router.post("/likeygood", async (req, res) => {
    // console.log(req.userInfo);
    const userId = req.userInfo._id
    // console.log(userId);
    let formData
    let upnumobj = {};
    const {
        hotelId,
        sightId,
        foodId,
        haiId
    } = req.body;
    let addhouobj ={
        userId,
    }
    if(hotelId){
        addhouobj.hotelId = hotelId
        upnumobj._id = hotelId
        formData=Hotel;
    }else if(sightId){
        addhouobj.sightId = sightId
        formData=Scenic;
        upnumobj._id = sightId

    }else if(foodId){
        addhouobj.foodId = foodId
        formData=Delicacy;
        upnumobj._id = foodId

    }else{
        addhouobj.haiId = haiId
        formData=Tourism;
        upnumobj._id = haiId

    } 

    
    await Likeygood.create(addhouobj);
    await formData.findOneAndUpdate(upnumobj,{$inc:{likeygoodNum:1}});

    res.status(200).json({
        status:200,
        message:"点赞成功"
    });
})

// 删除点赞的数据
// '/api/likey/deletelikeygood'
router.delete('/deletelikeygood',async (req, res) =>{
    const userId = req.userInfo._id
    let formData;
    let dowmnumobj = {};
    let {
        hotelId,
        sightId,
        foodId,
        haiId
    } = req.query;

    let delhouobj ={
        userId,
    }
    if(hotelId){
        delhouobj.hotelId = hotelId
        formData = Hotel;
        dowmnumobj._id=hotelId
    }else if(sightId){
        delhouobj.sightId = sightId
        formData = Scenic;
        dowmnumobj._id=sightId
    }else if(foodId){
        delhouobj.foodId = foodId
        formData = Delicacy;
        dowmnumobj._id=foodId
    }else{
        delhouobj.haiId = haiId
        formData = Tourism;
        dowmnumobj._id=haiId
    }
    // console.log(delhouobj);
    await Likeygood.findOneAndDelete(delhouobj);

    await formData.findOneAndUpdate(dowmnumobj,{$inc:{likeygoodNum:-1}});
    res.status(200).json({
        status:200,
        message:"取消点赞"
    });
})






module.exports = router